<?php

namespace App\Http\Controllers\Admin\Setting\Wechat;

use App\Http\Controllers\Admin\Controller;
use App\Repositories\Wechat\WechatDraftRepository;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Inertia\Response;

class WeChatDraftController extends Controller
{
    protected array $validateMessages = [
        'article_type.*' => '请输入输入文章类型',
        'title.*' => '请输入输入文章标题',
        'author.*' => '请输入输入文章作者',
        'digest.*' => '请输入输入文章摘要',
        'content.*' => '请输入输入文章内容',
        'content_source_url.*' => '请输入输入文章内容来源链接',
        'thumb_media_id.*' => '请输入输入文章封面',
        'thumb_media_url.*' => '请输入输入文章封面',
        'image_info.*' => '请输入输入文章封面图片信息',
        'status.*' => '请输入正确的状态',
        'sort.*' => '请输入排序',
    ];

    public function __construct(Request $request, WechatDraftRepository $repository)
    {
        $this->request = $request;
        $this->repository = $repository;
    }

    public function index(): Response
    {
        $list = $this->repository->pagination();

        return $this->inertia('setting/wechat/draft/index', [
            'data' => $list->items(),
            'pagination' => [
                'total' => $list->total(),
                'page' => $list->currentPage(),
                'pageSize' => $list->perPage(),
            ],
        ]);
    }

    public function show($id): array
    {
        return [
            'data' => $this->repository->getById($id),
        ];
    }

    /**
     * @throws ValidationException
     */
    public function store(): JsonResponse
    {
        $params = $this->validate(
            $this->request,
            [
                'article_type' => 'required|string',
                'title' => 'required|string',
                'author' => 'nullable|string',
                'cover' => 'nullable|string',
                'content' => 'nullable|string',
                'content_source_url' => 'nullable|string',
                'thumb_media_id' => 'nullable|string',
                'thumb_media_url' => 'nullable|string',
                'image_info' => 'nullable|array',
                'status' => 'required|string',
                'sort' => 'required|integer',
            ],
            $this->validateMessages
        );
        $params['need_open_comment'] = 1;
        $params['only_fans_can_comment'] = 0;
        $this->repository->create($params);

        return $this->responseCreated();
    }

    public function update($id): JsonResponse
    {
        $params = $this->validate(
            $this->request,
            [
                'article_type' => 'required|string',
                'title' => 'required|string',
                'author' => 'nullable|string',
                'cover' => 'nullable|string',
                'content' => 'nullable|string',
                'content_source_url' => 'nullable|string',
                'thumb_media_id' => 'nullable|string',
                'thumb_media_url' => 'nullable|string',
                'image_info' => 'nullable|array',
                'status' => 'required|string',
                'sort' => 'required|integer',
            ],
            $this->validateMessages
        );

        $row = $this->repository->getById($id);
        $row->update($id, $params);

        return $this->responseSuccess();
    }

    public function destroy($id): JsonResponse
    {
        $row = $this->repository->getById($id);
        $row->delete();

        return $this->responseSuccess();
    }
}
